/**
 * 
 */
package vue.popup;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;

import model.preprocessor.Satellite_1_0Parameter;
import model.solver.RSatParameter;

/**
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class IGSatelliteDialogParameter extends IGDialogParameter implements ActionListener {
	private static final long		serialVersionUID	= 1L;

	public static final String		OUT_DESCRIPTION		= "Output file: ";
	public static final String		OUT_BUTTON			= "Set output file";
	public static final String		DEFAULT_OUT_NAME	= "newcnf.cnf";


	private Satellite_1_0Parameter	param;

	// private JButton timeoutButton;
	private JPanel					timeoutPanel;
	private JLabel					timeoutDescritpion;


	public IGSatelliteDialogParameter(Satellite_1_0Parameter param) {
		this.param = param;
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		JPanel panel = new JPanel(new GridLayout(0, 1));

		// timeout
		timeoutPanel = new JPanel(new GridLayout(1, 2));

		timeoutDescritpion = new JLabel(TIMEOUT_DESCRIPTION);
		timeoutPanel.add(timeoutDescritpion);
		timeoutPanel.add(timeoutField);

		panel.add(timeoutPanel);

		add(panel);

		// out
		add(outPanel);

		refresh();

	}

	@Override
	public void refresh() {
		timeoutField.setText("" + param.getTimeOut());
		if (param.getOut() == null)
			outField.setText(lastDirectory.getPath() + "/");
		else
			outField.setText(param.getOut());

	}

	@Override
	public boolean checkAndSetParameter() {
		// TODO A tester
		boolean good = true;

		// timeout
		String timeout = timeoutField.getText();
		if (timeout.isEmpty())
			param.setTimeOut(RSatParameter.NO_DELAY);
		else
			try
			{
				param.setTimeOut(Integer.parseInt(timeout));
			} catch (Exception e)
			{
				timeoutPanel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.RED));
				good = false;
			}

		// out
		if (new File(outField.getText()).exists())
		{
			outPanel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.RED));
			good = false;
		}
		else
			param.setOut(outField.getText());

		return good;
	}


}
